<?php
namespace Admin\Controller\Chats;
use Admin\Controller\Auto\AutoController;
class ChatsController extends AutoController {
	public function index(){
			$u=M('user');
			$user=$u->alias('u')
			->field('u.*,p.pic')
			->join('left join think_personal p on p.uid=u.id')
			->where('u.auto=1')
			->select();
			// echo '<pre>';
			// var_dump($user);exit;
			$this->assign('user',$user);
			$this->display('/chats/chats/index');
		
		// $c=M('chats');
		// $cc=$c->query('SELECT * from think_chats where uid='.session('home_id').' or uid='.I('get.uid').'  ORDER by c_time desc limit 9');
		// $this->display('/chats/chats/index');
	}
	public function add(){
		if(I('post.content')){
			$m=M('chats');
			$m->uid=session('id');
			$m->yid=I('post.yid');
			$m->content=I('post.content');
			$m->c_time=date('Y-m-d H:i:s',time());
			if($m->add()){
				echo 'yes';
			}else{
				echo 'no';
			}
		}else{
			echo 'null';
		}
	}
	public function touxiang(){
		header('content-type:text/event-stream');
		$p=M('personal');
		$personal=$p->where('uid='.I('get.uid'))->find();
		$sess = M('personal')->where('uid='.session('id'))->find();
		$c=M('chats');
		$cc=$c->query('SELECT * from think_chats where uid='.session('id').' or uid='.I('get.uid').'  ORDER by c_time desc limit 9');
		// echo json_encode($cc);
		
		$arr=array();
		foreach($cc as &$v){
			if($v['uid']==$v['yid']){
				continue ;
			}
			if($v['yid']==session('id') || $v['yid']==I('get.uid')){
			// echo json_encode();
				$v['sessionid']=session('id');
				$v['u_name']=$personal['name'];
				$v['f_name']=$sess['name'];
				$v['u_pic']=$personal['pic'];
				$v['f_pic']=$sess['pic'];
				$arr[]=$v;
			}
		}
		$data=[];
		$i=count($arr)-1;
		foreach($arr as $v){
			$data[$i]=$v;
			$i--;
		}
		// echo json_encode($data);
			echo json_encode($data);
			// echo 'data:'.json_encode($data)."\n\n";
			
		
		return ;
	}
}
?>